package com.bingo.jdbc;

import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import com.mysql.jdbc.Driver;

public class JdbcUtils {
	private static Properties props = null;

	//只在JdbcUtils类被加载时执行一次
	static{
		//给props进行初始化，即加载dbconfig.properties文件到props对象中 
		try {
			//将文件转化为输入流
			InputStream in = JdbcUtils.class.getClassLoader().
					getResourceAsStream("dbconfig.properties");
			
			props = new Properties();
			props.load(in);
		} catch (IOException e) {
			// TODO: handle exception
			throw new RuntimeException(e);
		}

		try {
			//加载驱动 
			Class.forName(props.getProperty("driverClassName"));
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		}
	}
	public static Connection getConnection() throws ClassNotFoundException, SQLException, IOException{
		//返回Connection
		return DriverManager.getConnection(props.getProperty("url"),
				props.getProperty("username"),props.getProperty("password"));
	}
	
	
}
